package com.yww.datastructure.deque;

import java.util.Iterator;

/**
 * @author yww
 * @description 双向环形链表实现的双端队列
 * @since 2024/2/5 14:32
 */
public final class LinkedListDeque<E> implements Deque<E> {
    private final int capacity;
    private int size;
    // 哨兵
    private final Node<E> sentinel;

    public LinkedListDeque() {
        this(Integer.MAX_VALUE);
    }

    public LinkedListDeque(int capacity) {
        this.capacity = capacity;
        sentinel = new Node<>(null, null, null);
        sentinel.next = sentinel;
        sentinel.pre = sentinel;
    }

    @Override
    public boolean offerFirst(E e) {
        if (isFull()) {
            return false;
        }
        // a add b
        Node<E> a = this.sentinel;
        Node<E> b = sentinel.next;
        Node<E> added = new Node<>(a, e, b);
        a.next = added;
        b.pre = added;
        size++;
        return true;
    }

    @Override
    public boolean offerLast(E e) {
        if (isFull()) {
            return false;
        }
        // a added b
        Node<E> b = sentinel;
        Node<E> a = b.pre;
        Node<E> added = new Node<>(a, e, b);
        a.next = added;
        b.pre = added;
        size++;
        return true;
    }

    @Override
    public E pollFirst() {
        if (isEmpty()) {
            return null;
        }
        // a removed b
        Node<E> a = this.sentinel;
        Node<E> removed = a.next;
        Node<E> b = removed.next;
        a.next = removed.next;
        b.pre = a;
        size--;
        return removed.val;
    }

    @Override
    public E pollLast() {
        if (isEmpty()) {
            return null;
        }
        // a removed b
        Node<E> b = this.sentinel;
        Node<E> removed = this.sentinel.pre;
        Node<E> a = removed.pre;
        a.next = b;
        b.pre = a;
        size--;
        return removed.val;
    }

    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.next.val;
    }

    @Override
    public E peekLast() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.pre.val;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == capacity;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> p = sentinel.next;

            @Override
            public boolean hasNext() {
                return p != sentinel;
            }

            @Override
            public E next() {
                E val = p.val;
                p = p.next;
                return val;
            }
        };
    }

    private static class Node<E> {
        private Node<E> pre;
        private final E val;
        private Node<E> next;

        public Node(Node<E> pre, E val, Node<E> next) {
            this.pre = pre;
            this.val = val;
            this.next = next;
        }
    }
}
